package com.dnake.utils;


import com.wx.android.common.util.BASE64Utils;

/**
 * @author haoyuliu
 * @since 2017/7/21
 */

public class PassWordUtils {
    /**
     * 创建密码
     * @return
     */
    public static String creatPassword(){
        final int pwdLength = 6;
        String pwd = null;//密码
        String date = null;//日期
        String macAddress = AppUtils.getMacAddress();//门口机设备地址（默认12位16进制）
        macAddress = Long.parseLong(macAddress,16)+"";//转成10进制
        macAddress = macAddress.substring(macAddress.length()-6,macAddress.length());//取后六位
        date = DateUtil.currentDate().replaceAll("-","").substring(2);//日期格式170721

        pwd = (Integer.parseInt(macAddress)+ Integer.parseInt(date))/DateUtil.dayOfWeek()+"";//设备地址加上日期/当前周几
        //密码有可能出现5位 取当天所在周几补位
        if (pwd.length()<pwdLength){
            pwd = DateUtil.dayOfWeek()+pwd;
        }
        return pwd;
    }

    /**
     * 解密二维码
     * @param str
     * @return
     */
    public static String decodeIdentity(String str){
        String identity;
        identity = BASE64Utils.decodeBase64(convertString(str));
        return identity;
    }
    /**
     * 大小写转换
     * @param str
     * @return
     */
    private static String convertString(String str){
        char[] ch = str.toCharArray();
        StringBuffer sbf = new StringBuffer();
        for(int i=0; i< ch.length; i++){
            if(ch[i] <= 122 && ch[i] >= 97){
                //转大写
                ch[i] -= 32;
            }else if (ch[i] <= 90 && ch[i] >= 65){
                //转小写
                ch[i] += 32;
            }
            sbf.append(ch[i]);
        }
        return sbf.toString();
    }
}
